/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.functional;

/**
 *
 * @author Laurent WOUTERS
 */
public class FunctionalLoader implements xowl.interpreter.frontends.Loader {
    static final String name = "Functional Loader";
    
    @Override public xowl.lang.owl2.Ontology loadOntology(xowl.utils.logging.Logger logger, String docName, java.io.Reader reader, xowl.interpreter.frontends.OntologyListener listener) {
        String content = null;
        try { content = xowl.utils.Files.read(reader); }
        catch (java.io.IOException ex) {
            logger.error(name, "Error while reading " + docName + ": " + ex.getMessage(), ex);
            return null;
        }
        
        FParser parser = new FParser();
        FElementNode node = null;
        xowl.lang.owl2.Ontology onto = null;

        try { node = (FElementNode)parser.parse(docName, content); }
        catch (FParserException ex) {
            logger.error(name, "Error while parsing " + docName + ": " + ex.getMessage(), ex);
            return null;
        }
        
        if (node != null) {
            for (FNode child : node.getChildren()) {
                if (child.getType() == FNodeType.Element) {
                    FElementNode elem = (FElementNode)child;
                    if (elem.getName().equals(xowl.interpreter.frontends.VocOWL2.ontoOntology)) {
                        OntologyLoader loader = new OntologyLoader(docName, listener);
                        try { onto = loader.load(logger, elem); }
                        catch (FParserException ex) {
                            logger.error(name, "Error while loading " + docName + ": " + ex.getMessage(), ex);
                            return null;
                        }
                    }
                }
            }
        }
        return onto;
    }
    
    @Override public void loadQueries(xowl.utils.logging.Logger logger, String docName, java.io.Reader reader, xowl.interpreter.frontends.QueriesListener listener) {
        String content = null;
        try { content = xowl.utils.Files.read(reader); }
        catch (java.io.IOException ex) {
            logger.error(name, "Error while reading " + docName + ": " + ex.getMessage(), ex);
            return;
        }
        
        FParser parser = new FParser();
        FElementNode node = null;

        try { node = (FElementNode)parser.parse(docName, content); }
        catch (FParserException ex) {
            logger.error(name, "Error while parsing " + docName + ": " + ex.getMessage(), ex);
            return;
        }
        
        if (node != null) {
            for (FNode child : node.getChildren()) {
                if (child.getType() == FNodeType.Element) {
                    FElementNode elem = (FElementNode)child;
                    if (elem.getName().equals(xowl.interpreter.frontends.VocOWL2.ontoOntology)) {
                        QueriesLoader loader = new QueriesLoader(docName, listener);
                        try { loader.load(logger, elem); }
                        catch (FParserException ex) {
                            logger.error(name, "Error while loading " + docName + ": " + ex.getMessage(), ex);
                            return;
                        }
                    }
                }
            }
        }
    }
}
